﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Entities.Commodities;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using JSShop.Model;
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;


    public class YB_Sore_CategoryList : WebControl
    {
        private int categoryId;
        
        private string BindList()
        {
            int.TryParse(this.Context.Request.QueryString["categoryId"], out this.categoryId);
            StringBuilder sb = new StringBuilder();
            List<JSShop.Model.Hishop_Categories> maxSubCategories = new List<Hishop_Categories>();
            string classstyle = "";

            //staticize 获取参数
            int feature;
            int brand;
            int filter;
            int rank;
            int.TryParse(this.Page.Request.QueryString["feature"], out feature);
            int.TryParse(this.Page.Request.QueryString["brand"], out brand);
            int.TryParse(this.Page.Request.QueryString["filter"], out filter);
            int.TryParse(this.Page.Request.QueryString["rank"], out rank);

            if (this.categoryId != 0)
            {


                string url = this.Page.Request.Url.ToString();
                //顶级分类
                if (url.IndexOf("oldpage") != -1 && url.IndexOf("toprewrite") == -1)
                {
                    maxSubCategories = new Juse.Interface.CategoryBrowser().GetSubCategories(this.categoryId, 0);
                }
                else
                {
                    maxSubCategories = new Juse.Interface.CategoryBrowser().GetSubCategoriesR(this.categoryId, 0);
                }

                if (maxSubCategories.Count <= 0)
                {
                    maxSubCategories = new Juse.Interface.CategoryBrowser().GetSubCategories(0, 0);
                }
                else
                {
                    if (this.categoryId == maxSubCategories[0].ParentCategoryId)
                    {
                        classstyle = "class=\"kkokk\"";
                    }
                    else
                    {
                        classstyle = "";
                    }

                    if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                    {
                        sb.AppendFormat("<a href=\"{0}?oldpage\" {1} >全部</a>", Globals.GetSiteUrls().SubCategory((int)maxSubCategories[0].ParentCategoryId, maxSubCategories[0].ParentRewriteName), classstyle);
                    }
                    else
                    {
                        sb.AppendFormat("<a href=\"{0}\" {1} >全部</a>", StaticizeSubCategory((int)maxSubCategories[0].ParentCategoryId, maxSubCategories[0].ParentRewriteName, 0, "", feature, brand, filter, rank, 0), classstyle);
                    }
                    //StaticizeSubCategory(Convert.ToInt32(Eval("CategoryId")),(string)Eval("RewriteName"), Convert.ToInt32(Eval("ParentCategoryId")),(string)Eval("ParentRewriteName"),Convert.ToInt32(this.Page.Request.QueryString["feature"]),Convert.ToInt32(this.Page.Request.QueryString["brand"]),Convert.ToInt32(this.Page.Request.QueryString["filter"]),Convert.ToInt32(this.Page.Request.QueryString["rank"]),0)%>'
                }
                foreach (Hishop_Categories v in maxSubCategories)
                {
                    if (this.categoryId == v.CategoryId)
                    {
                        classstyle = "class=\"kkokk\"";
                    }
                    else
                    {
                        classstyle = "";
                    }
                    if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                    {
                        sb.AppendFormat("<a href=\"{0}\" {2}>{1}</a>", Globals.GetSiteUrls().SubCategory((int)v.ParentCategoryId, v.RewriteName, v.ParentRewriteName), v.Name, classstyle);
                    }
                    else
                    {

                        sb.AppendFormat("<a href=\"{0}\" {2} >{1}</a>", StaticizeSubCategory(v.CategoryId, v.RewriteName, (int)v.ParentCategoryId, v.ParentRewriteName, feature, brand, filter, rank, 0), v.Name, classstyle);

                    }
                }
            }
            else
            {
                maxSubCategories = new Juse.Interface.CategoryBrowser().GetSubCategories(0, 0);
                foreach (Hishop_Categories v in maxSubCategories)
                {
                    if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                    {
                        sb.AppendFormat("<a href=\"{0}?oldpage\" {2}>{1}</a>", Globals.GetSiteUrls().SubCategory((int)v.ParentCategoryId, v.RewriteName, v.ParentRewriteName), v.Name, classstyle);
                    }
                    else
                    {
                        sb.AppendFormat("<a href=\"{0}?oldpage\" {2}>{1}</a>", StaticizeSubCategory(v.CategoryId, v.RewriteName, (int)v.ParentCategoryId, v.ParentRewriteName, feature, brand, filter, rank, 0), v.Name, classstyle);
                    }
                }
            }

            return sb.ToString();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.BindList());
        }

        public virtual string StaticizeSubCategory(int categoryId, string categoryrewritename, int ParentCategoryId, string Parentcategoryname, int feature, int brand, int filter, int rank, int pn)
        {
            string temp = "";
            string tPrePath = Hidistro.Core.gloalvar.GetPrePath();
            
            if (string.IsNullOrEmpty(tPrePath) == false)
            {
                temp = "/" + tPrePath;
            }

            if (ParentCategoryId == 0)
            {
                if (categoryId == 0)
                {
                    if ((pn == 0) || (pn == 1))
                    {
                        //   temp = "Categoryid-0_SubCategoryid-0_feature-0_Brand-0_filter-0}_rank-0.html";
                        
                        temp += "C0S0F0B0F0R0.html";
                    }

                    else
                    {
                        temp += string.Format("C0S0F0B0F0R0P{0}.html", pn);
                        //temp = string.Format("Categoryid-0_SubCategoryid-0_feature-0_Brand-0_filter-0_rank-0_pn-{0}.html", pn);
                    }
                }
                else
                {
                    if ((pn == 0) || (pn == 1))
                    {
                        //   temp = string.Format("/{0}/Categoryid-{1}_SubCategoryid-0_feature-0_Brand-0_filter-0_rank-0.html", categoryrewritename, categoryId);
                        temp += string.Format("/{0}/C{1}S0F0B0F0R0.html", categoryrewritename, categoryId);
                    }
                    else
                    {
                        //temp = string.Format("/{0}/Categoryid-{1}_SubCategoryid-0_feature-0_Brand-0_filter-0_rank-0_pn-{2}.html", categoryrewritename, categoryId, pn);
                        temp += string.Format("/{0}/C{1}S0F0B0F0R0P{2}.html", categoryrewritename, categoryId, pn);
                    }
                }
            }
            else
            {
                if ((pn == 0) || (pn == 1))
                {
                    //temp = string.Format("/{0}/{1}/Categoryid-{2}_SubCategoryid-{3}_feature-0_Brand-0_filter-0_rank-0.html", Parentcategoryname, categoryrewritename, ParentCategoryId, categoryId);
                    temp += string.Format("/{0}/C{1}S{2}F0B0F0R0.html", Parentcategoryname, ParentCategoryId, categoryId);
                }
                else
                {
                    //temp = string.Format("/{0}/{1}/Categoryid-{2}_SubCategoryid-{3}_feature-0_Brand-0_filter-0_rank-0_pn-{4}.html", Parentcategoryname, categoryrewritename, ParentCategoryId, categoryId, pn);
                    temp += string.Format("/{0}/C{1}S{2}F0B0F0R0P{3}.html", Parentcategoryname, ParentCategoryId, categoryId, pn);
                }
            }
            return temp;
            //return this.urlData.FormatUrl("subCategory_Rewrite", new object[] { categoryId, rewriteName });
        }

    }
}

